﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InfoLogic.Models;
using System.Web.Security;

namespace InfoLogic.Controllers
{
    //[Authorize]
    public class BusinessController : Controller
    {
        private UserContext db = new UserContext();
        
        //
        // GET: /Business/
        public ActionResult Index()
        {
            String currentUserId = System.Web.Security.Membership.GetUser().ProviderUserKey.ToString();

            var business = db.Business
                
             .Where(b => b.UserId == currentUserId).ToList();

            return View(business); 

           // return View(db.Business.ToList());
        }

        //
        // GET: /Business/Details/5

        public ActionResult Details(Guid id )
        {
            Business business = db.Business.Find(id);
            if (business == null)
            {
                return HttpNotFound();
            }
            return View(business);
        }

        //
        // GET: /Business/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Business/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Business business)
        {
            if (ModelState.IsValid)
            {
                MembershipUser currentUser = System.Web.Security.Membership.GetUser(User.Identity.Name, userIsOnline: true);
                string username = currentUser.UserName;

                business.BusinessId = Guid.NewGuid();
                business.UserId = System.Web.Security.Membership.GetUser().ProviderUserKey.ToString();

                db.Business.Add(business);
               
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(business);
        }

        //
        // GET: /Business/Edit/5

        public ActionResult Edit(Guid id)
        {
            Business business = db.Business.Find(id);
            if (business == null)
            {
                return HttpNotFound();
            }
            return View(business);
        }

        //
        // POST: /Business/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Business business)
        {
            if (ModelState.IsValid)
            {
                db.Entry(business).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(business);
        }

        //
        // GET: /Business/Delete/5

        public ActionResult Delete(Guid id )
        {
            Business business = db.Business.Find(id);
            if (business == null)
            {
                return HttpNotFound();
            }
            return View(business);
        }

        //
        // POST: /Business/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Guid id)
        {
            Business business = db.Business.Find(id);
            db.Business.Remove(business);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult FindUserBusinesses(string UserId)
        {
            //var userid = (from e in db.Business
            //                   select e.UserId).FirstOrDefault();

            var business = (from b in db.Business
                            where b.UserId == UserId
                            select b).FirstOrDefault();

            return View(business);
        }

        public ActionResult OwnerBusiness()
        {
            ViewBag.Message = "Info logic Entry Form";
            return View();
        }
        //[Authorize(Roles = "Owner")]
        public ActionResult Manage(Guid id)
        {

            Business business = db.Business.Find(id);
            if (business == null)
            {
                return HttpNotFound();
            }
            return View(business);
        }

        public ActionResult ManageAfterSaleOwner(SellConfirmationViewModel SellConfirmationViewModel)
        {
            Business business = db.Business.Find(SellConfirmationViewModel.businessId);
            if (business == null)
            {
                return HttpNotFound();
            }
            return View(business);
        }

        [Authorize(Roles = "Owner, Manager")]
        public ActionResult ManagerOperations(Business p_business)
        {
            Business business = db.Business.Find(p_business.BusinessId);
            if (business == null)
            {
                return HttpNotFound();
            }
            return View(business);
        }

        [Authorize]
        public ActionResult SalesPersonOperations()
        {
            return View();
        }

        public ActionResult Reports(Business business)
        {

            Business Business = db.Business.Find(business.BusinessId);

            return View(Business);

        }

        public ActionResult ProductListReport(Business business)
        {
            Business Business = db.Business.Find(business.BusinessId);

            var productsQuery = db.Product
                            .Where(p => p.BusinessId == Business.BusinessId);

            return View(productsQuery.ToList());
        }

        public ActionResult ProductOutOfHandReport(Business business)
        {
            Business Business = db.Business.Find(business.BusinessId);

            var productsQuery = db.Product
                            .Where(p => p.BusinessId == Business.BusinessId && p.quantityOnHand <= 0);

            return View(productsQuery.ToList());
        }

        public ActionResult SalesReport(Business business)
        {
            Business Business = db.Business.Find(business.BusinessId);

            return View(Business);
        }

        public ActionResult ManagerIndex(CurrentUser currentUser)
        {
            System.Web.Security.Membership.GetAllUsers();

            var business = (from b in db.Business
                           from m in db.Manager
                           where m.userId == b.UserId && b.BusinessId == m.businessid
                           select b).Distinct();
            return View(business); 
        }

        public ActionResult DailySalesReport(Business business)
        {
            int soldday= DateTime.Now.Day;
            int soldMonth = DateTime.Now.Month;
            int soldYear = DateTime.Now.Year;
            Sales sales = new Models.Sales(); 
            String dbDateOfSale = String.Format("{0:M/d/ yyyy}", sales.DateOfSale);

            var query = (from s in db.Sales
                         from p in db.Product
                         where s.productId == p.ProductId && p.BusinessId == business.BusinessId && s.DateOfSale.Day == soldday 
                         && s.DateOfSale.Month == soldMonth && s.DateOfSale.Year == soldYear
                        
                         select new SalesReportViewModel
                         {
                             soldDate = s.DateOfSale,
                             productName = p.productName,
                             sellingPrice = p.sellingPrice,
                             purchasePrice = p.purchasePrice,
                         }
                        );


            return View(query.ToList());
          
        }

        public ActionResult MonthlySalesReport(Business business)
        {
            int soldMonth = DateTime.Now.Month;
            int soldYear = DateTime.Now.Year;
            Sales sales = new Models.Sales(); 
            String dbDateOfSale = String.Format("{0:dddd, MMMM d, yyyy}", sales.DateOfSale);
            var query = (from s in db.Sales
                         from p in db.Product
                         where s.productId == p.ProductId && p.BusinessId == business.BusinessId && s.DateOfSale.Month == soldMonth
                         && s.DateOfSale.Year == soldYear
                         select new SalesReportViewModel
                         {
                             soldDate = s.DateOfSale,
                             productName = p.productName,
                             sellingPrice = p.sellingPrice,
                             purchasePrice = p.purchasePrice,
                         }
                        );


            return View(query.ToList());
        }

        public ActionResult YearlySalesReport(Business business)
        {
            int soldYear = DateTime.Now.Year;
            Sales sales = new Models.Sales();
            String dbDateOfSale = String.Format("{0:dddd, MMMM d, yyyy}", sales.DateOfSale);
            var query = (from s in db.Sales
                         from p in db.Product
                         where s.productId == p.ProductId && p.BusinessId == business.BusinessId && s.DateOfSale.Year == soldYear

                         select new SalesReportViewModel
                         {
                             soldDate = s.DateOfSale,
                             productName = p.productName,
                             sellingPrice = p.sellingPrice,
                             purchasePrice = p.purchasePrice,
                         }
                        );


            return View(query.ToList());
        }
    }
}